package com.apes.scm.masterdata.party.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.EventUtil;
import com.apes.scm.masterdata.party.model.CustomerBusiness;
import com.apes.scm.masterdata.party.repository.CustomerBusinessRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 客户业务关系业务层
 * @author: wsj
 * @time: 2020/7/9 17:22
 */
@Service("customerBusinessService")
public class CustomerBusinessService extends DomainService {
    @Autowired
    private CustomerBusinessRepository businessRepository;


    public CustomerBusiness save(SimpleRequest request) {
        CustomerBusiness customerBusiness = request.getO(CustomerBusiness.class);
        List<CustomerBusiness> businessRepositoryAll = businessRepository.findAll(JpaDsl.toCriteriaByEq("accessProvider.id", customerBusiness.getAccessProvider().getId(),
                "client.id",  customerBusiness.getClient().getId(), "businessType.id", customerBusiness.getBusinessType().getId()));
        if(businessRepositoryAll.size() != 0) throw new RuntimeException("接入商，客户，业务类型存在相同，无法创建，请确认！");
        return businessRepository.saveAndFlush(customerBusiness);
    }


    public CustomerBusiness update(SimpleRequest request) {
        CustomerBusiness customerBusiness = request.getO(CustomerBusiness.class);
        return businessRepository.saveAndFlush(customerBusiness);

    }


    public CustomerBusiness delete(SimpleRequest request) throws Exception  {
        CustomerBusiness customerBusiness = request.getO(CustomerBusiness.class);
        customerBusiness.setValid(false);
        return  businessRepository.saveAndFlush(customerBusiness);
    }

    /**
     * App同步接车单
     */
    public List<CustomerBusiness> queryCustomerBusiness(SimpleRequest request){
        JSONObject jsonObject = request.getJO();
        Assert.isTrue(!StringUtils.isEmpty(jsonObject.getString("timestamp")), "时间戳不能为空!!!");
        Date timestamp = new Date(jsonObject.getLong("timestamp"));
        String id = jsonObject.getString("id"); //  最后更新ID
        int maxNum = jsonObject.getInteger("maxNum"); //单次请求最大条数
        return businessRepository.queryCustomerBusiness(timestamp, id, PageRequest.of(0, maxNum));
    }

    public void synPickUpCarToApp(SimpleRequest request) {
        Map newMap = new HashMap();
        newMap.put("method","cm.customerBusiness.queryCustomerBusiness");
        newMap.put("timestamp",new Date());
        // 调用App接口 通知接口
        String notifyTopic = "event:pi.app.synAppNotify";
        EventUtil.sendSynEvent(this, notifyTopic, newMap);

    }
}
